package jaux.tank.generator.jd.components;

import java.awt.Dimension;
import java.util.function.Consumer;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class JdTextArea extends JdLabeledInput<String[], Object> {
	
	private static final long serialVersionUID = 1L;
	private JTextArea textArea;
	
	public JdTextArea(String label, String[] text) {
		super(label, text);
		setSize(0, 100);
	}

	@Override
	public JComponent createInputComponent(Object  param) {
		textArea = new JTextArea();
		textArea.setRows(3);
		return new JScrollPane(textArea);
	}

	@Override
	public String[] getValue() {
		return this.textArea.getText().split("\n");
	}

	@Override
	public void setValue(String[] value) {
		this.textArea.setText(String.join("\n", value));

	}

	@Override
	public void registerChangeHandler(Consumer<String[]> changeHandler) {
		textArea.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void removeUpdate(DocumentEvent e) {
				changeHandler.accept(getValue());

			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				changeHandler.accept(getValue());
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
				changeHandler.accept(getValue());

			}
		});
	}

	public void setEditable(boolean b) {
		this.textArea.setEditable(b);
	}
	
	public void setRows(int rows) {
		this.textArea.setRows(rows);
	}
}
